const state = {
  typePage: {},
  typeList: [],
  list: [],
  page: {},
  commentList: [],
  commentPage: {},
  questionList: [],
  questionPage: {}
}

const actions = {
  saveTypes: ({ commit }, payload) => {
    commit('SAVETYPES', payload)
  },
  saveList: ({ commit }, payload) => {
    commit('SAVELIST', payload)
  },
  saveComment: ({ commit }, payload) => {
    commit('SAVECOMMENTLIST', payload)
  },
  saveQuestion: ({ commit }, payload) => {
    commit('SAVEQUESTIONLIST', payload)
  }
}

const mutations = {
  SAVETYPES: (state, payload) => {
    state.typeList = payload.result
    state.typePage = {
      number: payload.number,
      total: payload.resultNum,
      size: 10
    }
  },
  SAVELIST: (state, payload) => {
    if (payload.result.length === 0) {
      state.list = []  
    } else {
      state.list = payload.result.map(item => {
        return {
          id: item.id,
          publicAt: item.public_at,
          title: item.title,
          subTitle: item.sub_title,
          isTop: item.is_top,
          content: item.content,
          typeId: item.type_id,
          origin: item.origin,
          author: item.author,
          state: item.state,
          imgUrl: item.img_url,
          attachName: item.attach_name,
          attachUrl: item.attach_url,
          description: item.description,
          clickTimes: item.click_times
        }
      })
    }
    state.page = {
      number: payload.number,
      total: payload.resultNum,
      size: 10
    }
  },
  SAVECOMMENTLIST: (state, payload) => {
    if (payload.result.length > 0) {
      state.commentList = payload.result.map(item => {
        return {
          id: item.id,
          content: item.content,
          decisionId: item.decision_id
        }
      })
    } else {
      state.commentList = []
    }
    
    state.commentPage = {
      number: payload.number,
      size: 10,
      total: payload.resultNum
    }
  },
  SAVEQUESTIONLIST: (state, payload) => {
    state.questionList = payload.result
    state.questionPage = {
      number: payload.number,
      size: 10,
      total: payload.resultNum
    }
  }
}

export default {
  state,
  actions,
  mutations
}